#define _CRT_SECURE_NO_WARNINGS
#include"queue.h"

void QueueInit(Queue* pq)//初始化
{
	assert(pq);
	pq->head = pq->tail = NULL;
}


void QueueDestroy(Queue* pq)//销毁
{
	assert(pq);
	QueueNode* cur = pq->head;
	while (cur != NULL)
	{
		QueueNode* next = cur->next;
		free(cur);
		cur = next;
	}

	pq->head = NULL;
	pq->tail = NULL;

}

void QueuePush(Queue* pq, QDdataType x)//入队
{
	assert(pq);
	QueueNode* newNode = (QueueNode*)malloc(sizeof(QueueNode));
	if (newNode == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	newNode->data = x;
	newNode->next = NULL;
	//是否第一次入队
	if (pq->head == NULL)
	{
		pq->head = pq->tail = newNode;
	}
	//一个以上的节点
	else
	{
		pq->tail->next = newNode;
		pq->tail = pq->tail->next;
	}

}

void QueuePop(Queue* pq)//出对
{
	assert(pq);
	/*assert(pq->head != NULL);*/
	assert(!QueueEmpty(pq));
	
	QueueNode* next = pq->head->next;
	free(pq->head);
	pq->head = next;
	 //如果全部都出队了，尾指针就是野指针了
	if (pq->head == NULL)
	{
		pq->tail = NULL;
	}

}

QDdataType QueueFront(Queue* pq)//对头
{
	assert(pq);
	/*assert(pq->head);*/
	assert(!QueueEmpty(pq));
	return pq->head->data;
}

QDdataType QueueBack(Queue* pq)//对尾
{
	assert(pq);
	//assert(pq->tail);
	assert(!QueueEmpty(pq));
	return pq->tail->data;
}

int QueueSize(Queue* pq)//对的长度大小
{
	assert(pq);
	QueueNode* cur = pq->head;
	int size = 0;
	while (cur != NULL)
	{
		size++;
		cur = cur->next;
	}
	return size;
}

bool QueueEmpty(Queue* pq)//判空
{
	assert(pq);
	return pq->head == NULL;
}
